package com.taplinker.client.im.rpc.response;

import com.taplinker.client.AppConfigFactory;
import com.taplinker.client.App;
import com.taplinker.client.im.domain.ImDataCenter;
import com.taplinker.client.im.protocol.TapLinkerConversation;
import com.taplinker.client.im.rpc.Cmd016;
import com.taplinker.client.im.rpc.IMListener;
import com.taplinker.client.im.rpc.body.Cmd1004Rep;
import com.taplinker.core.rpc.Result;
import com.taplinker.core.util.json.JsonUtil;

public class Cmd1004  implements ImResponse{

	@Override
	public void yes(Result result) {
		Cmd1004Rep value = JsonUtil.jsonToObject(result.object.toString(), Cmd1004Rep.class);
		TapLinkerConversation con = ImDataCenter.getInstance().removeConversationByIdFromPub(value.getConversationId());
		
		if(value.getTo() == AppConfigFactory.getInstance().getUserInfo().getUserid()) {
			if(con != null){
				ImDataCenter.getInstance().addToPrivateQueue(con);
			} else {
				new Cmd016(value.getConversationId(), new IMListener() {
						
					@Override
					public void handleMessage(TapLinkerConversation con) {
						if(con.getUnreadMessages()>0) {
							ImDataCenter.getInstance().setCrmNum(ImDataCenter.getInstance().getPriNum() + 1);
						}
						ImDataCenter.getInstance().addToPrivateQueue(con);
						
					}
				});
			}
		}
		
		((App) AppConfigFactory.getInstance().getApplication()).notifyMessage(
				"您收到来至（"
				+ value.getFrom()
				+ "）转接会话，备注："
				+ value.getContent());
	}

	@Override
	public String getCommand() {
		return "1004";
	}

}